package com.world.rickcloudendpoint.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @description: swagger 2 配置
 * @author: jlsong
 * @date: 2021/12/04 16:16:02
 * @version: 1.0
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Value("${spring.application.name:business Spring}")
    private String swaggerTopic;

    @Bean
    public Docket api() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.world.rickcloudendpoint"))
//                .paths(petstorePaths())
//                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .pathMapping("/")
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
                ;

        return docket;
    }


    private Predicate<String> petstorePaths() {
//        return PathSelectors.regex("/rickcloud/*")
//                .or(regex("/**"))
//                .or(regex(".*/v3/api-doc.*"))
//                .or(regex(".*/api/user.*")
//                        .or(regex("/error.*")));
        return null;
    }


    private ApiInfo apiInfo() {
        Contact contact = new Contact("business", "1843232323", "business@gmail.com");
        return new ApiInfo(swaggerTopic, "SpringBoot + Swagger + JdbcTemplate", "V2.0",
                "No terms of service", contact, "google", "https://www.google.com", new ArrayList<>());
    }

    /**
     * 配置认证模式 名为“token”，类型为“header”
     *
     * @return
     */
//    private List<ApiKey> securitySchemes() {
//        return Lists.newArrayList(new ApiKey("Authorization", "token", "header"));
//    }

    /**
     * 配置认证上下文 登录、注册不需要配置token
     *
     * @return
     */
//    private List<SecurityContext> securityContexts() {
//        return Lists.newArrayList(SecurityContext.builder().securityReferences(defaultAuth())
//                .forPaths(PathSelectors.any()).build());
//    }

//    private List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
//    }

}
